<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
  
    <!-- 组件整合，其他组件需要定义在这个div内部 -->
    <SearchForm
    :searchData="pagination"
    @handleSearch="handleSearch"
    @handleReset="handleReset"
    @handleClear="handleClear"
    ></SearchForm>

    <TableList
    :listData="listData"
    :pagination="pagination"
    :total="total"
    @getCurrent="getCurrent"
    @handleBulid="handleBulid"
    @handleEdit="handleEdit"
    @handleClickDelete="handleClickDelete"
    @handleForbidden="handleForbidden"
    ></TableList>

    <DialogFrom
    ref="FromData"
    :visible="visible"
    :title="title"
    :data="DialogFromdata"
    :planData="planData"
    @handleAdd="handleAdd"
    @handleClose="handleClose"
    @handleEditForm="handleEditForm"
    ></DialogFrom>


     <!-- 删除弹层 -->
     <Delete
      :visible="dialogDeleteVisible"
      :title="dialogDelettitle"
      :deleteText="deleteText"
      @handleDelete="handleDelete"
      @handleClose="OperateDialogClose"
    ></Delete>
    <!-- end -->
    <!-- 禁用弹层 -->
    <Forbidden
      :visible="ForbiddenVisible"
      :text="Forbiddentitle"
      @handleSubmit="handleSubmit"
      @handleClose="ForbiddenhandleClose"
    ></Forbidden>
  </div>
</template>

<script setup lang="ts">
import { ref,onMounted } from "vue";
import TableList from './components/TableList.vue'
import SearchForm from './components/SearchForm.vue'
import DialogFrom from './components/DialogFrom.vue'
import {getLevelList,levelAdd,getnursAllPlanList,getLevelDetails,levelUpdate,levelDelete,levelStatus} from '@/api/serve'
// 删除弹层
import Delete from '@/components/OperateDialog/index.vue'
// 禁用弹窗
import Forbidden from '@/components/Forbidden/index.vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { getDataList } from "@/api/intelligence";


// ----------------------------------------定义相应对象
const listData=ref([])
const total=ref()
const pagination=ref({
  pageNum:1,
  pageSize:10,
})
const visible=ref(false)
const DialogFromdata=ref({})
const title=ref('')
const planData=ref([])
const FromData=ref(null)
// ====================================================================分页查询
const getList=async ()=>{
   const res = await getLevelList(pagination.value)
   listData.value = res.data.records
   total.value=res.data.total
}
onMounted(() => {
  getList()
  selAllPlan()
})
const selAllPlan=async ()=>{
   const res =await getnursAllPlanList()
   planData.value=res.data
}

// ================================================================条件搜索
const getCurrent=(val)=>{
  pagination.value.pageNum=val.current
  pagination.value.pageSize=val.pageSize
  getList()
}

const handleSearch=()=>{
  getList()
}
const handleReset=()=>{
    pagination.value={
    pageNum:1,
    pageSize:10,
  }
  getList()
}
const handleClear=(val)=>{
  if(val === 'name'){
    delete pagination.value.name
  }else{
    delete pagination.value.status
  }
}
// ==================================================================新增护理等级
const handleBulid=()=>{
  visible.value=true
  title.value='新增'
}
const handleAdd=async (val)=>{
  const res =await levelAdd(val)
  if(res.code === 200){
    MessagePlugin.success('新增等级成功')
    visible.value=false
    getList()
    FromData.value.handleClear()
  }else{
    MessagePlugin.error(res.msg)
  }
}
const handleClose=()=>{
  visible.value=false
}
// ================================================================编辑
const handleEdit=(val)=>{
  visible.value=true
  title.value='编辑'
  getDataList(val.id)
}
const getDataList=async (val)=>{
  DialogFromdata.value = (await getLevelDetails(val)).data
  DialogFromdata.value.planId = DialogFromdata.value.planId+''
}

const handleEditForm=async (val)=>{
  const res =await levelUpdate(val)
  if(res.code === 200){
    MessagePlugin.success('编辑等级成功')
    visible.value=false
    getList()
  }else{
    MessagePlugin.error(res.msg)
  }
}
// =======================================================================删除
const delId=ref()
const dialogDeleteVisible=ref(false)
const dialogDelettitle=ref('')
const deleteText=ref('护理等级')

const handleClickDelete=(val)=>{
  dialogDeleteVisible.value=true
  delId.value=val.id
  dialogDelettitle.value='确认删除'
}
const handleDelete = async () =>{
  const res = await levelDelete(delId.value)
  if(res.code === 200){
    //提示，编辑成功
    MessagePlugin.success('编辑成功')
    getList()
    OperateDialogClose()
  }else{
    MessagePlugin.error(res.msg);
  }
}
const OperateDialogClose=()=>{
  dialogDeleteVisible.value=false
  delId.value=ref()
}
// ===================================================================修改
const ForbiddenVisible=ref(false)
const Forbiddentitle=ref('')
const stopstatus =ref({})
const handleForbidden=(val)=>{
  ForbiddenVisible.value=true
  Forbiddentitle.value='护理项目'
  stopstatus.value=val
}
const handleSubmit=async (val)=>{
  if(stopstatus.value.status === 1){
    stopstatus.value.status=0
  }else{
    stopstatus.value.status=1
  }
  const res = await levelStatus(stopstatus.value)
  if(res.code === 200){
    MessagePlugin.success('禁用成功')
    getList()
    ForbiddenhandleClose()
  }else{
    MessagePlugin.error(res.msg);
  }
}
const ForbiddenhandleClose=()=>{
  ForbiddenVisible.value=false
  stopstatus.value=ref({})
}



</script>